<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>j2ee(1) | heit0ng</title><meta name="author" content="heit0ng"><meta name="copyright" content="heit0ng"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="咳咳，本系列主要是j2ee里的JSP部分(虽然公司企业都不用，还是要学(悲))，主要是为了小结一下学校每次上机实验的内容 idea为2021.3版本 tomcat9.0.73 jdk版本是11 meavn3.6.3 mysql5.0以上 至于怎么配置环境就不在此赘述了，网上都有详细教程 如何创建项目也请自行查找详细教程，谢谢 关于源码等上机实验完全结束后我会一并上传至Gitee，谢谢 关于 Htt">
<meta property="og:type" content="article">
<meta property="og:title" content="j2ee(1)">
<meta property="og:url" content="https://heit0ng.gitee.io/2023/03/22/j2ee(1)/index.html">
<meta property="og:site_name" content="heit0ng">
<meta property="og:description" content="咳咳，本系列主要是j2ee里的JSP部分(虽然公司企业都不用，还是要学(悲))，主要是为了小结一下学校每次上机实验的内容 idea为2021.3版本 tomcat9.0.73 jdk版本是11 meavn3.6.3 mysql5.0以上 至于怎么配置环境就不在此赘述了，网上都有详细教程 如何创建项目也请自行查找详细教程，谢谢 关于源码等上机实验完全结束后我会一并上传至Gitee，谢谢 关于 Htt">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s2.loli.net/2023/03/22/3KqCOLtHBdvhMFe.webp">
<meta property="article:published_time" content="2023-03-22T12:20:17.683Z">
<meta property="article:modified_time" content="2023-03-22T12:30:47.613Z">
<meta property="article:author" content="heit0ng">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2023/03/22/3KqCOLtHBdvhMFe.webp"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://heit0ng.gitee.io/2023/03/22/j2ee(1)/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'j2ee(1)',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-03-22 20:30:47'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://s2.loli.net/2022/01/28/RkXzVpjubg3yGEI.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">0</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div></div><hr/></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://s2.loli.net/2023/03/22/3KqCOLtHBdvhMFe.webp')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">heit0ng</a></span><div id="menus"><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">j2ee(1)</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-03-22T12:20:17.683Z" title="发表于 2023-03-22 20:20:17">2023-03-22</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-03-22T12:30:47.613Z" title="更新于 2023-03-22 20:30:47">2023-03-22</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="j2ee(1)"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>咳咳，本系列主要是j2ee里的JSP部分(虽然公司企业都不用，还是要学(悲))，主要是为了小结一下学校每次上机实验的内容</p>
<p>idea为2021.3版本 tomcat9.0.73 jdk版本是11 meavn3.6.3 mysql5.0以上</p>
<p>至于怎么配置环境就不在此赘述了，网上都有详细教程</p>
<p>如何创建项目也请自行查找详细教程，谢谢</p>
<p>关于源码等上机实验完全结束后我会一并上传至Gitee，谢谢</p>
<h3 id="关于-HttpServlet、GenericServlet-和-Servlet-的关系"><a href="#关于-HttpServlet、GenericServlet-和-Servlet-的关系" class="headerlink" title="关于 HttpServlet、GenericServlet 和 Servlet 的关系"></a>关于 HttpServlet、GenericServlet 和 Servlet 的关系</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">public interface Servlet &#123;</span><br><span class="line">    void init(ServletConfig var1) throws ServletException;</span><br><span class="line"></span><br><span class="line">    ServletConfig getServletConfig();</span><br><span class="line"></span><br><span class="line">    void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;</span><br><span class="line"></span><br><span class="line">    String getServletInfo();</span><br><span class="line"></span><br><span class="line">    void destroy();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这是一个servlet类(确切的的说是接口)，提供了5种方法，3个生命周期和两个普通方法，都需要对方法进行重写</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">public abstract class GenericServlet implements Servlet, ServletConfig, Serializable &#123;</span><br><span class="line">    private static final long serialVersionUID = 1L;</span><br><span class="line">    private transient ServletConfig config;</span><br><span class="line"></span><br><span class="line">    public GenericServlet() &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void destroy() &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public String getInitParameter(String name) &#123;</span><br><span class="line">        return this.getServletConfig().getInitParameter(name);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public Enumeration&lt;String&gt; getInitParameterNames() &#123;</span><br><span class="line">        return this.getServletConfig().getInitParameterNames();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public ServletConfig getServletConfig() &#123;</span><br><span class="line">        return this.config;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public ServletContext getServletContext() &#123;</span><br><span class="line">        return this.getServletConfig().getServletContext();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public String getServletInfo() &#123;</span><br><span class="line">        return &quot;&quot;;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void init(ServletConfig config) throws ServletException &#123;</span><br><span class="line">        this.config = config;</span><br><span class="line">        this.init();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void init() throws ServletException &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void log(String message) &#123;</span><br><span class="line">        this.getServletContext().log(this.getServletName() + &quot;: &quot; + message);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void log(String message, Throwable t) &#123;</span><br><span class="line">        this.getServletContext().log(this.getServletName() + &quot;: &quot; + message, t);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;</span><br><span class="line"></span><br><span class="line">    public String getServletName() &#123;</span><br><span class="line">        return this.config.getServletName();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>而GenericServlet是一个抽象类，实现的是Servlet接口，对其中的init() destory service()提供默认实现</p>
<p>implements此类时，必须将抽象类serviece进行实现</p>
<p>而实际上我们用得最多的是<strong>HttpServlet</strong>，它也是一个抽象类，进一步继承并封装了 GenericServlet，使得使用更加简单方便，因为扩展了 Http  的内容，所以还需要使用 HttpServletRequest 和 HttpServletResponse，这两个类分别是  ServletRequest 和 ServletResponse 的子类。代码如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">abstract class HttpServlet extends GenericServlet&#123;</span><br><span class="line">   //HttpServlet中的service()</span><br><span class="line">   protected void service(HttpServletRequest httpServletRequest,</span><br><span class="line">                       HttpServletResponse httpServletResponse)&#123;</span><br><span class="line">        //该方法通过httpServletRequest.getMethod()判断请求类型调用doGet() doPost()</span><br><span class="line">   &#125;</span><br><span class="line"> </span><br><span class="line">   //必须实现父类的service()方法</span><br><span class="line">   public void service(ServletRequest servletRequest,ServletResponse servletResponse)&#123;</span><br><span class="line">      HttpServletRequest request;</span><br><span class="line">      HttpServletResponse response;</span><br><span class="line">      try&#123;</span><br><span class="line">         request=(HttpServletRequest)servletRequest;</span><br><span class="line">         response=(HttpServletResponse)servletResponse;</span><br><span class="line">      &#125;catch(ClassCastException)&#123;</span><br><span class="line">         throw new ServletException(&quot;non-http request or response&quot;);</span><br><span class="line">      &#125;</span><br><span class="line">      //调用service()方法</span><br><span class="line">      this.service(request,response);</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>HttpServlet 中对原始的 Servlet 中的方法都进行了默认的操作，不需要显式的销毁初始化以及 service()，在  HttpServlet 中，自定义了一个新的 service() 方法，其中通过 getMethod() 方法判断请求的类型，从而调用  doGet() 或者 doPost() 处理 get,post 请求，使用者只需要继承 HttpServlet，然后重写 doPost() 或者 doGet() 方法处理请求即可。</p>
<h3 id="WebServlet注解与配置web-xml"><a href="#WebServlet注解与配置web-xml" class="headerlink" title="@WebServlet注解与配置web.xml"></a>@WebServlet注解与配置web.xml</h3><p>两者都可以配置Servlet</p>
<h5 id="WebServlet"><a href="#WebServlet" class="headerlink" title="@WebServlet"></a>@WebServlet</h5><p><img src="https://s2.loli.net/2023/03/22/AzU1wEvDGQytRu8.png" alt="Webservlet注解.png"></p>
<p>使用方法参考</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">@WebServlet(name = &quot;MyServlet&quot;,  //Servlet名</span><br><span class="line">	urlPatterns = &quot;/serv02&quot;,   //Servlet访问的url</span><br><span class="line">	loadOnStartup = 1,      //Servlet创建的时机</span><br><span class="line">	initParams = &#123;         //ServletConfig的相关配置</span><br><span class="line">			@WebInitParam(name=&quot;name&quot;, value=&quot;YYH&quot;), </span><br><span class="line">			@WebInitParam(name=&quot;pwd&quot;, value=&quot;123456&quot;)</span><br><span class="line">	&#125;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<p>此方法直接在Servlet类中使用，代码量少，配置简单，与其他类互不干扰，但是Servlet较多时，不便于查找与修改</p>
<p>实例:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">@WebServlet(value=&#123;&quot;/servletc&quot;,&quot;/servletd&quot;&#125;)//此处即为注解</span><br><span class="line">public class servlet01 extends GenericServlet &#123;</span><br><span class="line">    @Override</span><br><span class="line">    public void destroy() &#123;</span><br><span class="line">        super.destroy();</span><br><span class="line">        System.out.println(&quot;destory被调用了....&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">    @Override</span><br><span class="line">    public void init() throws ServletException &#123;</span><br><span class="line">        super.init();</span><br><span class="line">        System.out.println(&quot;init被调用了.....&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">    @Override</span><br><span class="line">    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException &#123;</span><br><span class="line">        System.out.println(&quot;service被调用了.....&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="web-xml"><a href="#web-xml" class="headerlink" title="web.xml"></a>web.xml</h5><p>该配置处于web文件下</p>
<p>实例:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;web-app xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;</span><br><span class="line">         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">         xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot;</span><br><span class="line">         version=&quot;4.0&quot;&gt;</span><br><span class="line">    &lt;servlet&gt;</span><br><span class="line">        &lt;servlet-name&gt;servlet02&lt;/servlet-name&gt; //指定的类名</span><br><span class="line">        &lt;servlet-class&gt;hello.servlet02&lt;/servlet-class&gt;//包与类名</span><br><span class="line">    &lt;/servlet&gt;</span><br><span class="line">    &lt;servlet-mapping&gt;</span><br><span class="line">        &lt;servlet-name&gt;servlet02&lt;/servlet-name&gt;</span><br><span class="line">        &lt;url-pattern&gt;/servlete&lt;/url-pattern&gt;//url设置 可设置多个</span><br><span class="line">        &lt;url-pattern&gt;/servletf&lt;/url-pattern&gt;</span><br><span class="line">        &lt;url-pattern&gt;/servletg&lt;/url-pattern&gt;</span><br><span class="line">    &lt;/servlet-mapping&gt;</span><br><span class="line">&lt;/web-app&gt;</span><br></pre></td></tr></table></figure>

<p>此方法便于集中管理修改，但是代码比较繁琐，不易于理解。</p>
<h3 id="多个servlet任务移交转发情况"><a href="#多个servlet任务移交转发情况" class="headerlink" title="多个servlet任务移交转发情况"></a>多个servlet任务移交转发情况</h3><p>此处以三个servlet为例子</p>
<p>分别是servlet05</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">@WebServlet(&quot;/servlet05&quot;)</span><br><span class="line">public class servlet05 extends HttpServlet &#123;</span><br><span class="line">    @Override</span><br><span class="line">    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;</span><br><span class="line">        //防止中文乱码</span><br><span class="line">        resp.setContentType(&quot;text/html;charset=UTF-8&quot;);</span><br><span class="line">        </span><br><span class="line">        //用于客户端传递过来的参数</span><br><span class="line">        String username = req.getParameter(&quot;username&quot;);</span><br><span class="line"></span><br><span class="line">        username = username+&quot;servlet05&quot;;</span><br><span class="line">        req.setAttribute(&quot;username&quot;,username);//将目标保存在作用域中，下一节会详细解释</span><br><span class="line">        </span><br><span class="line">        System.out.println(&quot;任务05时,username=&quot;+username);</span><br><span class="line">        System.out.println(&quot;模拟演示完成了业务的一部分：任务05&quot;);</span><br><span class="line">        </span><br><span class="line">        PrintWriter out =resp.getWriter();//接收反应里面的内容</span><br><span class="line">        out.println(&quot;我是05，我完成了任务，给出响应&quot;);//内容可打印在页面上</span><br><span class="line">        </span><br><span class="line">        //申请转交对象</span><br><span class="line">        RequestDispatcher rd = req.getRequestDispatcher(&quot;/servlet06&quot;);</span><br><span class="line">        //完成转交</span><br><span class="line">        rd.forward(req,resp);</span><br><span class="line">    &#125;</span><br><span class="line">    @Override</span><br><span class="line">    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p> servlet06</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">@WebServlet(&quot;/servlet06&quot;)</span><br><span class="line">public class servlet06 extends HttpServlet &#123;</span><br><span class="line">    @Override</span><br><span class="line">    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;</span><br><span class="line">        resp.setContentType(&quot;text/html;charset=UTF-8&quot;);</span><br><span class="line"></span><br><span class="line">        Object username = req.getAttribute(&quot;username&quot;);</span><br><span class="line">        String username2 = (String) username;</span><br><span class="line">        username2 = username2 + &quot;servlet06&quot;;</span><br><span class="line">        req.setAttribute(&quot;username&quot;,username2);</span><br><span class="line">        System.out.println(&quot;任务06时,username=&quot;+username2);</span><br><span class="line"></span><br><span class="line">        System.out.println(&quot;模拟演示完成了业务的一部分：任务06&quot;);</span><br><span class="line">        PrintWriter out =resp.getWriter();</span><br><span class="line">        out.println(&quot;我是06，我完成了任务，给出响应&quot;);</span><br><span class="line">        //申请转交对象</span><br><span class="line">        RequestDispatcher rd = req.getRequestDispatcher(&quot;/servlet07&quot;);</span><br><span class="line">        //完成转交</span><br><span class="line">        rd.forward(req,resp);</span><br><span class="line">    &#125;</span><br><span class="line">    @Override</span><br><span class="line">    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>servlet07</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">@WebServlet(&quot;/servlet07&quot;)</span><br><span class="line">public class servlet07 extends HttpServlet &#123;</span><br><span class="line">    @Override</span><br><span class="line">    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;</span><br><span class="line">        resp.setContentType(&quot;text/html;charset=UTF-8&quot;);</span><br><span class="line"></span><br><span class="line">        Object username = req.getAttribute(&quot;username&quot;);</span><br><span class="line">        String username3 = (String) username;</span><br><span class="line">        username3 = username3 + &quot;servlet07&quot;;</span><br><span class="line">        req.setAttribute(&quot;username&quot;,username3);</span><br><span class="line"></span><br><span class="line">        System.out.println(&quot;任务07时,username=&quot;+username3);</span><br><span class="line"></span><br><span class="line">        System.out.println(&quot;模拟演示完成了业务的一部分：任务07&quot;);</span><br><span class="line">        PrintWriter out =resp.getWriter();</span><br><span class="line">        out.println(&quot;我是07，我完成了最后的任务，给出响应&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">    @Override</span><br><span class="line">    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>结果显示</p>
<p><img src="https://s2.loli.net/2023/03/22/GHia59MYJetfbkX.png" alt="页面.png"></p>
<p><img src="https://s2.loli.net/2023/03/22/vbhFMALK5XGYelP.png" alt="控制台.png"></p>
<p>有null的原因是没有传参数</p>
<p>关于其中的一些代码解释</p>
<p>   String username = req.getParameter(“username”);</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;form action=&quot;xxxxxxx.do&quot; &gt;</span><br><span class="line">&lt;input name=&quot;name&quot; value=&quot;哈哈&quot;/&gt;</span><br><span class="line">&lt;input type=&quot;submit&quot; value=&quot;提交&quot;/&gt;</span><br><span class="line">&lt;/form&gt;</span><br><span class="line">这个form提交请求后，在你的action中</span><br><span class="line">String name = request.getparameter(&quot;name&quot;); </span><br><span class="line">那么name的值就是“哈哈”</span><br><span class="line">   它是一种取参数的方法。把jsp文件中的数据读取到出来。然后就可以封装利用起来。</span><br></pre></td></tr></table></figure>

<p>你也可以直接在访问页面时直接在链接中给出参数</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">如http://localhost:8080/demo_war_exploded/servlet05?uername=aaa</span><br></pre></td></tr></table></figure>

<p>?后面就是参数</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">heit0ng</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://heit0ng.gitee.io/2023/03/22/j2ee(1)/">https://heit0ng.gitee.io/2023/03/22/j2ee(1)/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://heit0ng.gitee.io" target="_blank">heit0ng</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://s2.loli.net/2023/03/22/3KqCOLtHBdvhMFe.webp" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/03/22/j2ee(2)/"><img class="prev-cover" src="https://s2.loli.net/2023/03/23/mTDuOeL67MFtgZs.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">j2ee(2)</div></div></a></div><div class="next-post pull-right"><a href="/2022/05/16/C%E8%AF%AD%E8%A8%80%E5%9B%BE%E4%B9%A6%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/"><img class="next-cover" src="https://s2.loli.net/2022/05/16/ae4E7n2D3I5mqkh.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">C语言图书管理系统</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://s2.loli.net/2022/01/28/RkXzVpjubg3yGEI.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">heit0ng</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="card-info-data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">0</div></a></div><div class="card-info-data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B3%E4%BA%8E-HttpServlet%E3%80%81GenericServlet-%E5%92%8C-Servlet-%E7%9A%84%E5%85%B3%E7%B3%BB"><span class="toc-number">1.</span> <span class="toc-text">关于 HttpServlet、GenericServlet 和 Servlet 的关系</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#WebServlet%E6%B3%A8%E8%A7%A3%E4%B8%8E%E9%85%8D%E7%BD%AEweb-xml"><span class="toc-number">2.</span> <span class="toc-text">@WebServlet注解与配置web.xml</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#WebServlet"><span class="toc-number">2.0.1.</span> <span class="toc-text">@WebServlet</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#web-xml"><span class="toc-number">2.0.2.</span> <span class="toc-text">web.xml</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%9A%E4%B8%AAservlet%E4%BB%BB%E5%8A%A1%E7%A7%BB%E4%BA%A4%E8%BD%AC%E5%8F%91%E6%83%85%E5%86%B5"><span class="toc-number">3.</span> <span class="toc-text">多个servlet任务移交转发情况</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/03/22/j2ee(2)/" title="j2ee(2)"><img src="https://s2.loli.net/2023/03/23/mTDuOeL67MFtgZs.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="j2ee(2)"/></a><div class="content"><a class="title" href="/2023/03/22/j2ee(2)/" title="j2ee(2)">j2ee(2)</a><time datetime="2023-03-22T15:50:01.860Z" title="发表于 2023-03-22 23:50:01">2023-03-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/22/j2ee(1)/" title="j2ee(1)"><img src="https://s2.loli.net/2023/03/22/3KqCOLtHBdvhMFe.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="j2ee(1)"/></a><div class="content"><a class="title" href="/2023/03/22/j2ee(1)/" title="j2ee(1)">j2ee(1)</a><time datetime="2023-03-22T12:20:17.683Z" title="发表于 2023-03-22 20:20:17">2023-03-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/05/16/C%E8%AF%AD%E8%A8%80%E5%9B%BE%E4%B9%A6%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/" title="C语言图书管理系统"><img src="https://s2.loli.net/2022/05/16/ae4E7n2D3I5mqkh.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="C语言图书管理系统"/></a><div class="content"><a class="title" href="/2022/05/16/C%E8%AF%AD%E8%A8%80%E5%9B%BE%E4%B9%A6%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/" title="C语言图书管理系统">C语言图书管理系统</a><time datetime="2022-05-16T14:28:31.730Z" title="发表于 2022-05-16 22:28:31">2022-05-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/04/07/C++qt/" title="C++Qt"><img src="https://s2.loli.net/2022/04/07/Cyg9WQLqSNxX7Av.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="C++Qt"/></a><div class="content"><a class="title" href="/2022/04/07/C++qt/" title="C++Qt">C++Qt</a><time datetime="2022-04-07T12:41:19.618Z" title="发表于 2022-04-07 20:41:19">2022-04-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/03/12/c++%E7%9A%84%E7%9F%A5%E8%AF%86(1)/" title="c++的知识(1)"><img src="https://s2.loli.net/2022/03/12/QrwRKYfGEmP3vVL.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="c++的知识(1)"/></a><div class="content"><a class="title" href="/2022/03/12/c++%E7%9A%84%E7%9F%A5%E8%AF%86(1)/" title="c++的知识(1)">c++的知识(1)</a><time datetime="2022-03-12T13:48:26.310Z" title="发表于 2022-03-12 21:48:26">2022-03-12</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By heit0ng</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>